package com.suao.goods.service.impl;

import com.suao.common.annotation.DataScope;
import com.suao.common.constant.DelFlagConst;
import com.suao.common.utils.DateUtils;
import com.suao.common.utils.SecurityUtils;
import com.suao.goods.domain.StorageSalesInfo;
import com.suao.goods.mapper.StorageSalesInfoMapper;
import com.suao.goods.service.IStorageSalesInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 仓库销售流水Service业务层处理
 *
 * @author tyz
 * @date 2023-03-08
 */
@Service
public class StorageSalesInfoServiceImpl implements IStorageSalesInfoService
{
    @Autowired
    private StorageSalesInfoMapper storageSalesInfoMapper;

    /**
     * 查询仓库销售流水
     *
     * @param id 仓库销售流水主键
     * @return 仓库销售流水
     */
    @Override
    public StorageSalesInfo selectStorageSalesInfoById(Long id)
    {
        return storageSalesInfoMapper.selectStorageSalesInfoById(id);
    }

    /**
     * 查询仓库销售流水列表
     *
     * @param storageSalesInfo 仓库销售流水
     * @return 仓库销售流水
     */
    @Override
    @DataScope(userAlias = "u")
    public List<StorageSalesInfo> selectStorageSalesInfoList(StorageSalesInfo storageSalesInfo)
    {
        return storageSalesInfoMapper.selectStorageSalesInfoList(storageSalesInfo);
    }

    /**
     * 新增仓库销售流水
     *
     * @param storageSalesInfo 仓库销售流水
     * @return 结果
     */
    @Override
    public int insertStorageSalesInfo(StorageSalesInfo storageSalesInfo)
    {
        storageSalesInfo.setCreateBy(String.valueOf(SecurityUtils.getUserId()));
        storageSalesInfo.setCreateTime(DateUtils.getNowDate());
        storageSalesInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        storageSalesInfo.setUpdateTime(DateUtils.getNowDate());
        return storageSalesInfoMapper.insertStorageSalesInfo(storageSalesInfo);
    }

    /**
     * 修改仓库销售流水
     *
     * @param storageSalesInfo 仓库销售流水
     * @return 结果
     */
    @Override
    public int updateStorageSalesInfo(StorageSalesInfo storageSalesInfo)
    {
        storageSalesInfo.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        storageSalesInfo.setUpdateTime(DateUtils.getNowDate());
        return storageSalesInfoMapper.updateStorageSalesInfo(storageSalesInfo);
    }

    /**
     * 批量删除仓库销售流水
     *
     * @param ids 需要删除的仓库销售流水主键
     * @return 结果
     */
    @Override
    public int deleteStorageSalesInfoByIds(Long[] ids)
    {
        int count = 0;
       for (int i = 0; i < ids.length; i++) {
           StorageSalesInfo temp = storageSalesInfoMapper.selectStorageSalesInfoById(ids[i]);
           temp.setDelFlag(DelFlagConst.DELETED);
           temp.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
           temp.setUpdateTime(DateUtils.getNowDate());
           storageSalesInfoMapper.updateStorageSalesInfo(temp);
           count++;
       }
       return  count;
        //return storageSalesInfoMapper.deleteStorageSalesInfoByIds(ids);
    }

    /**
     * 删除仓库销售流水信息
     *
     * @param id 仓库销售流水主键
     * @return 结果
     */
    @Override
    public int deleteStorageSalesInfoById(Long id)
    {
        StorageSalesInfo temp = storageSalesInfoMapper.selectStorageSalesInfoById(id);
        temp.setDelFlag(DelFlagConst.DELETED);
        temp.setUpdateBy(String.valueOf(SecurityUtils.getUserId()));
        temp.setUpdateTime(DateUtils.getNowDate());
        int count =  storageSalesInfoMapper.updateStorageSalesInfo(temp);
        return count;
        //return storageSalesInfoMapper.deleteStorageSalesInfoById(id);
    }
}
